package com.example.menuvirtualclient.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.menuvirtualclient.dbschema.InsertRows;
import com.example.menuvirtualclient.dbschema.TComment;
import com.example.menuvirtualclient.dbschema.TDish;
import com.example.menuvirtualclient.dbschema.TDishes_Comments;
import com.example.menuvirtualclient.dbschema.TDishes_Menues;
import com.example.menuvirtualclient.dbschema.TEnquiry;
import com.example.menuvirtualclient.dbschema.TMenu;
import com.example.menuvirtualclient.dbschema.TMenuType;
import com.example.menuvirtualclient.dbschema.TOrder;
import com.example.menuvirtualclient.dbschema.TOrderDetails_Comments;
import com.example.menuvirtualclient.dbschema.TOrderState;
import com.example.menuvirtualclient.dbschema.TOrder_Detail;
import com.example.menuvirtualclient.dbschema.TPrices;
import com.example.menuvirtualclient.dbschema.TTable;
import com.example.menuvirtualclient.dbschema.TUser;

public class Session extends SQLiteOpenHelper implements ISession {

	private static final String LOGTAG = "Session";
	private static SQLiteDatabase _dataBase;
	private static final String DATABASE_NAME = "MenuVirtual.db";
	private static final int DATABASE_VERSION = 1;	
	
	public Session(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void Close() {
		try{
			if(_dataBase != null)
				_dataBase.close();
			Log.i(LOGTAG, "Database closed.");
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, "Error closing the database.", ex);
			throw ex;
		}
	}

	public boolean Open() {
		boolean opened = false;
		try{
			_dataBase = getWritableDatabase();			
			opened = _dataBase.isOpen();
			Log.i(LOGTAG, "Database opened.");
		}
		catch(SQLiteException ex){
			opened = false;
			Log.e(LOGTAG, "Error opening the database.", ex);
			throw ex;
		}
		return opened;
	}

	public boolean IsOpen() {
		return _dataBase.isOpen();
	}

	public boolean TransactionInProcess() {
		return _dataBase.inTransaction();
	}

	public void BeginTransaction() {
		_dataBase.beginTransaction();
	}

	public void TransactionRoolBack() {
		//_dataBase.c
	}

	public void CommitTransaction() {
		_dataBase.setTransactionSuccessful();
	}

	public void EndTransaction() {
		_dataBase.endTransaction();	
	}

	public SQLiteDatabase Connection() {
		return _dataBase;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		try{
			db.execSQL(TMenuType.CreateTable());
			db.execSQL(TMenu.CreateTable());
			db.execSQL(TDish.CreateTable());
			db.execSQL(TDishes_Menues.CreateTable());
			db.execSQL(TUser.CreateTable());
			db.execSQL(TTable.CreateTable());
			db.execSQL(TOrder.CreateTable());
			db.execSQL(TOrder_Detail.CreateTable());
			db.execSQL(TComment.CreateTable());
			db.execSQL(TDishes_Comments.CreateTable());
			db.execSQL(TOrderDetails_Comments.CreateTable());
			db.execSQL(TOrderState.CreateTable());
			db.execSQL(TEnquiry.CreateTable());
			db.execSQL(TPrices.CreateTable());
			
			//Inserto registros
			db.execSQL(InsertRows.InsertUser2());
			db.execSQL(InsertRows.InsertUser3());
			db.execSQL(InsertRows.InsertTable1());
			db.execSQL(InsertRows.InsertTable2());
			db.execSQL(InsertRows.InsertTable3());
			/*			
			db.execSQL(InsertRows.InsertOrder1());
			db.execSQL(InsertRows.InsertOrder2());
			db.execSQL(InsertRows.InsertOrder3());
			
			db.execSQL(InsertRows.InsertOrderDetails1());
			db.execSQL(InsertRows.InsertOrderDetails2());
			db.execSQL(InsertRows.InsertOrderDetails3());
			*/
			db.execSQL(InsertRows.InsertEnquire1());
			db.execSQL(InsertRows.InsertEnquire2());
			db.execSQL(InsertRows.InsertEnquire3());			
			
		}
		catch(SQLiteException ex){
			Log.e("ERROR", ex.getMessage());
			throw ex;
		}		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		try{
			
		}
		catch(SQLiteException ex){
			throw ex;
		}
	}

}